package com.jingyanzi.resallocation.service.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;


import com.jingyanzi.resallocation.dao.ResAssistantMapper;
import com.jingyanzi.resallocation.dao.ResSalesgroupMapper;
import com.jingyanzi.resallocation.domain.AssistantDO;
import com.jingyanzi.resallocation.entity.ResAssistant;
import com.jingyanzi.resallocation.entity.ResAssistantExample;
import com.jingyanzi.resallocation.entity.ResAssistantExample.Criteria;
import com.jingyanzi.resallocation.entity.ResSalesgroup;
import com.jingyanzi.resallocation.query.AssistantQueryDO;
import com.jingyanzi.resallocation.result.ResultDO;
import com.jingyanzi.resallocation.result.ResultSupport;
import com.jingyanzi.resallocation.service.IAssistantService;
import com.jingyanzi.resallocation.util.BeanUtilsExtends;

@Service("assistantService")
public class AssistantServiceImpl extends BaseServiceImpl implements IAssistantService {

	@Autowired
	private ResAssistantMapper assistantMapper;
	@Autowired
	private ResSalesgroupMapper salesgroupMapper;


	

	public ResultDO edit(AssistantDO assistantDO) {
		ResAssistant assistant = new ResAssistant();

		ResultSupport result = BeanUtilsExtends.copy(assistant, assistantDO);
		if (!result.isSuccess())
			return result;
		int r = -1;

		try {
			r = assistantMapper.updateByPrimaryKeySelective(assistant);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改店员信息失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改店员信息失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO del(Integer[] id) {
		ResAssistant assistant = new ResAssistant();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		for (int i = 0; i < id.length; i++) {
			assistant.setId(id[i]);
			assistant.setIsDelete(-1);
			int r = -1;

			try {
				r = assistantMapper.updateByPrimaryKeySelective(assistant);
			} catch (Exception e) {
				e.printStackTrace();
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除店员信息失败");
				result.setSuccess(false);
				return result;
			}

			if (r < 1) {
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除店员信息失败");
				result.setSuccess(false);
				return result;
			}
		}

		return result;
	}

	public ResultDO get(Integer id) {
		ResAssistant assistant = new ResAssistant();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		try {
			assistant = assistantMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取店员信息失败");
			result.setSuccess(false);
			return result;
		}

		if (assistant == null) {
			result.setErrorCode(ResultDO.ASSISTANT_GET_ERROR);
			result.setErrorMsg("不存在该店员信息");
			result.setSuccess(false);
			return result;
		}
		AssistantDO assistantDO = getAssistantDO(assistant);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, assistantDO);
		return result;
	}

	public ResultDO query(AssistantQueryDO assistantQueryDO) {
		ResultSupport result = new ResultSupport();

		List<ResAssistant> list = null;

		ResAssistantExample example = new ResAssistantExample();

		Criteria createCriteria = example.createCriteria();

		if (assistantQueryDO.getMappingId() != null && assistantQueryDO.getMappingId() != 0) {
			createCriteria.andMappingIdEqualTo(assistantQueryDO.getMappingId());
		}
		if (assistantQueryDO.getGroupId() != null && assistantQueryDO.getGroupId() != 0) {
			createCriteria.andGroupIdEqualTo(assistantQueryDO.getGroupId());
		}
		if (assistantQueryDO.getType() != null && assistantQueryDO.getType() != 0) {
			createCriteria.andTypeEqualTo(assistantQueryDO.getType());
		}
		createCriteria.andIsDeleteEqualTo(1);
		List<AssistantDO> accountDiscountDos = null;

		if (assistantQueryDO.isPage()) {
			example.setLimitByPage(assistantQueryDO.getStart() + "," + assistantQueryDO.getPageRows());
			long count = assistantMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = assistantMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取店员信息失败");
			result.setSuccess(false);
			return result;
		}

		accountDiscountDos = getAssistantDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, accountDiscountDos);
		return result;
	}

}
